import axios from 'axios';
axios.defaults.timeout = 10000;
axios.defaults.baseURL = 'http://192.168.7.145:8080/';//这里是修改接口ip地址的
axios.defaults.headers['Content-Type'] = 'application/json'//全局配置请求头
//http request 拦截器(回调函数)
// axios.interceptors.request.use(
//   config => {
    // const token = document.cookie.split(";")[0];//注意使用的时候需要引入cookie方法，推荐js-cookie
    
    // config.data = JSON.stringify(config.data);
    // config.headers = {
    //   'Content-Type': 'application/json',
    // }
    // if (token) {
    //   config.params = { 'token': token }
    // }
//     console.log('config==>>',config)
//     return config;
//   },
//   error => {
//     return Promise.reject(err);
//   }
// );

axios.interceptors.request.use(function (config) {
  // 这里的config包含每次请求的内容
  let token = window.sessionStorage.getItem('token')
  // console.log('token99999999999',token)
  if (token) {
  // 添加headers
  config.headers.Authorization = token
  config.headers['Content-Type'] = 'application/json'
  } else {}
  console.log('config==>>',config)
  return config;
  }, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
  });

//http response 拦截器
axios.interceptors.response.use(
  response => {
    if (response.data.errCode == 2) {
      router.push({
        path: "/heilongjiang/heilongjiang",
        querry: { redirect: router.currentRoute.fullPath }//从哪个页面跳转
      })
    }
    return response;
  },
  error => {
    return Promise.reject(error)
  }
)


/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function fetch(url, params = {}) {
  return new Promise((resolve, reject) => {
    axios.get(url, {
      params: params
    })
      .then(response => {
        resolve(response.data);
      })
      .catch(err => {
        reject(err)
      })
  })
}


/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function post(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.post(url, data)
      .then(response => {
        resolve(response.data);
      }, err => {
        reject(err)
      })
  })
}

/**
* 封装patch请求
* @param url
* @param data
* @returns {Promise}
*/

export function patch(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.patch(url, data)
      .then(response => {
        resolve(response.data);
      }, err => {
        reject(err)
      })
  })
}

/**
* 封装put请求
* @param url
* @param data
* @returns {Promise}
*/

export function put(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.put(url, data)
      .then(response => {
        resolve(response.data);
      }, err => {
        reject(err)
      })
  })
}


/**
 * 封装DELETE请求
 * @param url
 * @param data
 * @returns {Promise}
 */

// export function cut(url,data = {}){
//   return new Promise((resolve,reject) => {
//     axios.delete(url,data)
//          .then(response => {
//            resolve(response.data);
//          },err => {
//            reject(err)
//          })
//   })
// }


export function cut(url, config) {
  return new Promise((resolve, reject) => {
    axios.delete(url, config)
      .then(response => {
        resolve(response.data);
      }, err => {
        reject(err)
      })
  })
}